package com.runshu.newsmartark.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.runshu.newsmartark.R;
import com.runshu.newsmartark.activity.RegisterAndRecognizeActivity;
import com.runshu.newsmartark.adapter.MyFaceAdapter;
import com.runshu.newsmartark.base.BaseLazyFragmentPlus;
import com.runshu.newsmartark.entity.FaceManagerBean;
import com.runshu.newsmartark.entity.UserInfoEntity;
import com.runshu.newsmartark.mvp.contract.CallBackObJValue;
import com.runshu.newsmartark.mvp.contract.FaceManagerContract;
import com.runshu.newsmartark.mvp.contract.GetUserListContract;
import com.runshu.newsmartark.mvp.model.FaceManagerModel;
import com.runshu.newsmartark.mvp.model.GetUserListModel;
import com.runshu.newsmartark.mvp.presenter.FaceManagerPresenter;
import com.runshu.newsmartark.mvp.presenter.GetUserListPresenter;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.util.common.ToastUtils;
import com.runshu.newsmartark.util.widget.ComChoseNameDia;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/8/27
 * @decs：
 */

public class FaceManagerFragment extends BaseLazyFragmentPlus implements CallBackObJValue, GetUserListContract.View, FaceManagerContract.View {
    @BindView(R.id.rela_add_face)
    RelativeLayout mRelaAddFace;
    @BindView(R.id.recy_face)
    RecyclerView mRecyFace;
    private MyFaceAdapter mAdapter;
    private List<UserInfoEntity> mData;
    private List<UserInfoEntity> userList;
    private GetUserListPresenter userListPresenter;
    private FaceManagerPresenter faceManagerPresenter;
    private String usercode;

    @Override
    protected int getLayoutId() {
        return R.layout.frg_face_manager;
    }

    @Override
    protected void initData() {
        userList = new ArrayList<>();
        mData = new ArrayList<>();
        mAdapter = new MyFaceAdapter(mData);
        mRecyFace.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        mRecyFace.setAdapter(mAdapter);
        mAdapter.bindToRecyclerView(mRecyFace);
        faceManagerPresenter.getFaceList();
        userListPresenter.getUserList();
        mAdapter.setEmptyView(R.layout.empty_view);
    }

    @Override
    protected void initView(View layoutInflateView) {
        userListPresenter = new GetUserListPresenter(this, new GetUserListModel());
        faceManagerPresenter = new FaceManagerPresenter(this, new FaceManagerModel());
    }

    @Override
    protected void bindListener() {

    }

    @OnClick(R.id.rela_add_face)
    public void onViewClicked() {

        //注册人脸页面
        ComChoseNameDia comChoseNameDia = new ComChoseNameDia(context, this, userList);
        comChoseNameDia.setOnClickListenerEnsure(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, RegisterAndRecognizeActivity.class);
                intent.putExtra("isAdd", true);
                intent.putExtra("usercode", usercode);
                startActivityForResult(intent, 101);
                comChoseNameDia.dismiss();
            }
        });
        comChoseNameDia.show();
    }

    @Override
    public void SendMessageValue(Object valueObj) {
        usercode = valueObj.toString();
    }

    @Override
    public void onGetFaceListSuccess(ResponseBean responseBean) {
        FaceManagerBean faceManagerBean = (FaceManagerBean) responseBean.pullData();
        if (faceManagerBean != null)
            mData.addAll(faceManagerBean.getList());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getUserListSuccess(ResponseBean responseBean) {
        userList.addAll((Collection<? extends UserInfoEntity>) responseBean.pullData());
    }

    @Override
    public void onError(String msg) {
        ToastUtils.showText(msg);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mData.clear();
        userList.clear();
        faceManagerPresenter.getFaceList();
        userListPresenter.getUserList();
    }
}